package com.dream.technology.controller;

import com.dream.technology.data.GenericRspDTO;
import com.dream.technology.db.entity.*;
import com.dream.technology.service.RcClassificationService;
import com.dream.technology.service.RcCustomerTagService;
import com.dream.technology.service.RcRuleCollectionService;
import com.dream.technology.service.RcRuleService;
import com.dream.technology.utils.RandomCode;
import com.dream.technology.vo.ErrorCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/risk/admin/classification")
public class RcClassificationController {

    private static final Logger logger = LoggerFactory.getLogger(RcClassificationController.class);

    @Autowired
    private RcClassificationService rcClassificationService;
    
    @Autowired
    private RcCustomerTagService rcCustomerTagService;
    
    @Autowired
    private RcRuleService rcRuleService;
    
    @Autowired
    private RcRuleCollectionService rcRuleCollectionService;

    /**
     * 用户标签分类列表接口
     * @param pageNum
     * @param pageSize
     * @param classifyName
     * @return
     */
    @GetMapping("/list")
    public GenericRspDTO<Object> getList(Integer pageNum, Integer pageSize,Integer classifyType, String classifyName) {
        RcClassificationQuery query=new RcClassificationQuery();
        RcClassificationQuery.Criteria criteria = query.createCriteria();
        if (StringUtils.isNotBlank(classifyName)){
            criteria.andClassifyNameLike(classifyName);
        }
        criteria.andClassifyTypeEqualTo(classifyType);
        criteria.andStatusEqualTo(false);
        query.setOrderByClause("enabled desc,create_date desc");
        PageHelper.startPage(pageNum,pageSize);
        List<RcClassification> rcClassificationList = rcClassificationService.selectByExample(query);
        PageInfo pageInfo = new PageInfo(rcClassificationList);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), pageInfo);
    }

    /**
     * 获取用户标签分类回显接口
     * @param id
     * @return
     */
    @GetMapping("/id")
    public GenericRspDTO<RcClassification> getRcClassificationById(Long id){

        RcClassification rcClassification = rcClassificationService.selectByPrimaryKey(id);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), rcClassification);
    }

    /**
     * 添加用户标签分类
     * @param rcClassification
     * @return
     */
    @PostMapping("/add")
    public GenericRspDTO<Object> addRcClassification(RcClassification rcClassification){

        String riskControlClaCode = RandomCode.getRiskControlClaCode();
        rcClassification.setClassifyCode(riskControlClaCode);
        rcClassification.setStatus(false);
        rcClassificationService.insert(rcClassification);

        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     * 根据分类类型 获取用户标签 ，规则 ，规则集 分类回显接口
     * 标签分类类型 0：用户标签 1：规则标签 2:规则集标签
     * @param type
     * @return
     */
    @GetMapping("/getByType")
    public GenericRspDTO<Map<String, Object>> getByType(int  type ){
    	Map<String, Object> map = new HashMap<String, Object>();
        RcClassificationQuery query = new RcClassificationQuery();
        RcClassificationQuery.Criteria criteria = query.createCriteria();
        criteria.andClassifyTypeEqualTo(type);
        criteria.andEnabledEqualTo(true);
        criteria.andStatusEqualTo(false);
        List<RcClassification> rcClassifications = rcClassificationService.selectByExample(query);
        map.put("list", rcClassifications);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), map);
    }

    /**
     * 修改用户标签分类接口
     * @param rcClassification
     * @return
     */
    @PostMapping("/update")
    public GenericRspDTO<Object> updateRcClassification(RcClassification rcClassification){
        rcClassificationService.updateByPrimaryKeySelective(rcClassification);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }


    /**
     *  删除用户标签接口 (逻辑删除)
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public GenericRspDTO<Object> deleteRcClassification(Long id, Boolean status, Boolean enabled){
        RcClassification rcClassification = new RcClassification();
        rcClassification.setId(id);
//        rcClassification.set
        if (status != null) {
        	rcClassification.setStatus(status);
		}
		if (enabled != null) {
			rcClassification.setEnabled(enabled);
		}
        rcClassificationService.updateByPrimaryKeySelective(rcClassification);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), null);
    }

    /**
     * 通过分类id，查询引用此分类的用户标签
     * @param id
     * @return
     */
    @GetMapping("/getCustomerTagByClassId")
    public GenericRspDTO<Integer> getByType(Long id){
    	RcCustomerTagQuery query = new RcCustomerTagQuery();
    	RcCustomerTagQuery.Criteria criteria = query.createCriteria();
    	criteria.andTagTypeIdEqualTo(id.intValue());
    	int count = rcCustomerTagService.countByExample(query);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }
    
    /**
     * 通过分类id，查询引用此分类的规则
     * @param id
     * @return
     */
    @GetMapping("/getRuleByClassId")
    public GenericRspDTO<Integer> getRuleByClassId(Integer id){
    	RcRuleQuery query = new RcRuleQuery();
    	RcRuleQuery.Criteria criteria = query.createCriteria();
    	criteria.andClassifyIdEqualTo(id);
    	int count = rcRuleService.countByExample(query);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }
    
    /**
     * 通过分类id，查询引用此分类的规则集
     * @param id
     * @return
     */
    @GetMapping("/getRuleCollectionByClassId")
    public GenericRspDTO<Integer> getRuleCollectionByClassId(Integer id){
    	RcRuleCollectionQuery query = new RcRuleCollectionQuery();
    	RcRuleCollectionQuery.Criteria criteria = query.createCriteria();
    	criteria.andClassifyIdEqualTo(id);
    	int count = rcRuleCollectionService.countByExample(query);
        return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }
    
    /**
     * 验证名称是否重复
     * @param name
     * @return
     */
    @GetMapping("/getByName")
    public GenericRspDTO<Object> getByName(String name, Integer id){
    	RcClassificationQuery query = new RcClassificationQuery();
    	RcClassificationQuery.Criteria criteria = query.createCriteria();
    	criteria.andClassifyNameEqualTo(name);
    	criteria.andClassifyTypeEqualTo(id);
    	int count = rcClassificationService.selectByExample(query).size();
    	return GenericRspDTO.newInstance(ErrorCode.SUCCESS.getErrorCode(), ErrorCode.SUCCESS.getErrorMsg(), count);
    }

}
